<?php
session_start();
header("Content-type:text/html;charset=utf-8");
require '../../../php/database_login.php';

$return = [];

function get_str($value, $str)
{
    if ($value == '不限') {
        return '';
    } else {
        if ($value == '未定义' || $value == 'no') {
            return " and (`" . $str . "` = '' or `" . $str . "` is null or `" . $str . "` ='no')";
        } else {
            return " and `" . $str . "` = '" . $value . "'";
        }
    }
}
$arr_column = ['category', 'color', 'style'];
$where = " where `id`<>''";
foreach ($arr_column as $i) {
    $where .= get_str($_POST['arr'][$i], $i);
}
if (!empty($_POST['arr']['value'])) {
    $where .= "";
}

$sql = "select count(*) from `multipack_brief`" . $where;
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
if ($row['count(*)'] == 0) {
    echo "empty";
    die;
}
$return['page_total'] = ceil($row['count(*)'] / 14);
$return['page_num'] = $_POST['arr']['page_num'];
$return['data_count'] = $row['count(*)'];

$str_order = " limit " . (($_POST['arr']['page_num'] - 1) * 14) . ", 14";
$arr_temp = [];


$sql = "select * from `multipack_brief`" . $where . $str_order;
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
    $return['detail']['brief'][] = $row;
    $arr_temp[] = $row['pack_id'];
}

$sql = "select * from `multipack_detail` where `pack_id` in ('" . implode("','", $arr_temp) . "') order by `order` asc";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
    $return['detail']['detail'][$row['pack_id']][] = $row;
}
echo json_encode($return);
